package com.zy.service.bs.impl;

import com.zy.dao.bs.ClubDao;
import com.zy.entity.bs.Club;
import com.zy.entity.enums.StatusEnum;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.pojo.query.bs.ClubQuery;
import com.zy.pojo.vos.bs.ClubSetWizardsVo;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.ClubService;
import com.zy.service.fileutil.FileUploadService;
import com.zy.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.List;

@Service("clubService")
public class ClubServiceImpl extends BaseServiceImpl<Club> implements ClubService {

	@Autowired
	private ClubDao clubDao;
	@Autowired
	private FileUploadService fileUploadService;
	@Value("${fileUploadPath}")
	private String fileUploadPath;


	@Override
	public PageVo pageClub(ClubQuery clubQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.clubDao.countClub(clubQuery), this.clubDao.listClub(clubQuery, start, limit));
	}

	@Override
	public void saveClub(List<MultipartFile> clubImgs, Club club) {
		long time = DateUtil.getTime();
		if(clubImgs.get(0).getSize() <= 0){
			throw new ServiceException("俱乐部主图不能为空！");
		}
		String clubImg;
		try {
			clubImg = fileUploadService.multipleUpload(clubImgs, "fileUploadPath");
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("俱乐部主图上传失败！");
		}
		club.setClubImg(clubImg);
		club.setCreateTime(time);
		club.setModifyTime(time);
		club.setStatus(StatusEnum.NORMAL.getValue());
		this.clubDao.save(club);
	}
	
	@Override
	public void updateClub(List<MultipartFile> clubImgs,Club club) {
		if(clubImgs.get(0).getSize() <= 0){
			Club club2 = this.clubDao.getByPrimaryKey(club.getId());
			try {
				fileUploadService.deleteImage(club2.getClubImg());
			} catch (IOException e1) {
				e1.printStackTrace();
				throw new ServiceException(fileUploadPath+"未找到图片");
			}
			String fileName;
			try {
				fileName = fileUploadService.multipleUpload(clubImgs, "fileUploadPath");
			} catch (IOException e) {
				e.printStackTrace();
				throw new ServiceException("俱乐部主图修改失败！");
			}
			club.setClubImg(fileName);
		}
		club.setModifyTime(DateUtil.getTime());
		this.clubDao.updateByPrimaryKeySelective(club);
	}

	@Override
	public void removeClub(long id) {
		try {
			this.clubDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Club club = new Club();
			club.setId(id);
			club.setModifyTime(DateUtil.getTime());
			club.setStatus(StatusEnum.DELETE.getValue());
			this.clubDao.updateByPrimaryKeySelective(club);
		}
	}

	@Override
	public void recover(long id) {
		Club club = new Club();
		club.setId(id);
		club.setModifyTime(DateUtil.getTime());
		club.setStatus(StatusEnum.NORMAL.getValue());
		this.clubDao.updateByPrimaryKeySelective(club);
	}

	@Override
	public PageVo pageClubForHomePage(Integer start, Integer limit) {
		return new PageVo(start, limit, this.clubDao.countClubForHomePage(), this.clubDao.getClubForHomePage(start,limit));
	}

	@Override
	public void saveClub(Club club) {
		this.clubDao.save(club);
	}

	@Override
	public void updateClub(Club club) {
		this.clubDao.updateByPrimaryKeySelective(club);
	}

	@Override
	public void updateClubSetWizards(ClubSetWizardsVo clubSetWizardsVo) {
	    Club club = new Club();
        BeanUtils.copyProperties(clubSetWizardsVo,club);
        club.setIsSet(1);
		this.clubDao.updateByPrimaryKeySelective(club);
	}

    @Override
    public List<Club> getMyClub(Long userId) {
        return clubDao.getMyClub(userId);
    }

    @Override
    public Club getClubInfo(Long id) {
        return clubDao.getClubInfo(id);
    }

    @Override
    public List<Club> getMyClubManager(Long userId) {
        return clubDao.getMyClubManager(userId);
    }
}
